/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.model.command;

import org.eclipse.gef.commands.Command;
import org.eclipse.uml2.uml.CombinedFragment;
import org.eclipse.uml2.uml.InteractionOperatorKind;

/**
 * Command to set the operator on CombinedFragments. The operator is given as a
 * String that this command must then parse and convert into a valid operator.
 * 
 * <p>
 * The following attributes must be set for the command to complete
 * successfully:
 * </p>
 * <ul>
 * <li>CombinedFragment
 * <li>OperatorString
 * </ul>
 * 
 * @author frankd@ifi.uio.no
 */
public class SetInteractionOperatorCommand extends Command {

	private CombinedFragment cfragment;
	private String operatorString;
	private InteractionOperatorKind oldOperator;

	public void setCombinedFragment(final CombinedFragment cfragment) {
		this.cfragment = cfragment;
	}

	public void setOperatorString(final String operatorString) {
		this.operatorString = operatorString;
	}

	@Override
	public void execute() {
		oldOperator = cfragment.getInteractionOperator();
		if (operatorString != null) {
			cfragment.setInteractionOperator(InteractionOperatorKind
					.get(operatorString.toLowerCase()));
		}
		// TODO Handle other expressions, and/or log when there are problems
	}

	@Override
	public void undo() {
		cfragment.setInteractionOperator(oldOperator);
		oldOperator = null;
	}

	@Override
	public void redo() {
		this.execute();
	}

	@Override
	public void dispose() {
		cfragment = null;
		operatorString = null;
		oldOperator = null;
	}
}
